//
//  CALayer+Animation.m
//  JQFY
//
//  Created by 代辉 on 2017/8/19.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "CALayer+Animation.h"

@implementation CALayer (Animation)

- (void)beginRotationWithDuration:(CFTimeInterval)duration
{
    [self removeAnimationForKey:@"rotation"];
    CABasicAnimation *animation = [[CABasicAnimation alloc] init];
    animation.fromValue = @(0);
    animation.toValue = @(M_PI * 2);
    animation.duration = duration;
    animation.keyPath = @"transform.rotation.z";
    animation.repeatCount = NSIntegerMax;
    animation.removedOnCompletion = NO;
    [self addAnimation:animation forKey:@"rotation"];
}

- (void)pauseAnimate
{
    CFTimeInterval pausedTime = [self convertTime:CACurrentMediaTime() fromLayer:nil];
    self.speed = 0.0;
    self.timeOffset = pausedTime;
}

- (void)resumeAnimate
{
    CFTimeInterval pausedTime = [self timeOffset];
    self.speed = 1.0;
    self.timeOffset = 0.0;
    self.beginTime = 0.0;
    CFTimeInterval timeSincePause = [self convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    self.beginTime = timeSincePause;
}

- (void)removeAnimation
{
    [self removeAllAnimations];
}
@end
